<?php
/**
 * Created by 王跃达.
 * User: 小马出行
 * Date: 2017/8/24
 * Time: 16:17
 */

namespace app\api\model;


use my\Define;
use think\Db;
use think\Model;

class WalletModel extends Model
{


    public function __construct()
    {
        parent::__construct();

    }

    public function getWalletByUid($uid)
    {
        $where['uid']=$uid;
        return Db::name("user_wallet")->where($where)->find();
    }

    public function addWallet($uid,$money){
        $wallet=$this->getWalletByUid($uid);
        if(empty($wallet)){
            Db::name("user_wallet")->insert(array("wallet_id"=>$wallet['wallet_id'],"uid"=>$uid,"wallet_money"=>$wallet['wallet_money']+$money));
        }else{
            Db::name("user_wallet")->where(array("wallet_id"=>$wallet['wallet_id']))->update(array("wallet_money"=>$wallet['wallet_money']+$money));
        }

    }

    public function updateWallet($map,$data)
    {
        Db::startTrans();
        try{
            Db::name("user_wallet")->where($map)->update($data);
            Db::commit();
            return true;
        }catch (\Exception $e){
            Db::rollback();
            return false;
        }
    }
}